package com.tony.pmp.server.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.tony.pmp.common.utils.Constant;
import com.tony.pmp.common.utils.PageUtil;
import com.tony.pmp.common.utils.QueryUtil;
import com.tony.pmp.model.entity.AttendRecordEntity;
import com.tony.pmp.model.mapper.AttendRecordMapper;
import com.tony.pmp.server.service.AttendRecordService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: AttendRecordServiceImpl
 * @Auther: 皮蛋布丁
 * @Date: 2021/08/03/15:07
 * @Description:
 */
@Service("attendRecordService")
public class AttendRecordServiceImpl extends ServiceImpl<AttendRecordMapper, AttendRecordEntity> implements AttendRecordService {
    
    /**
    * @Description: queryPage 获取考勤列表数据(分页，复杂模糊查询)
    * @Param: [params]
    * @return: com.tony.pmp.common.utils.PageUtil
    * @Author: 皮蛋布丁
    * @Date: 2021/8/3 15:21
    */
    @Override
    public PageUtil queryPage(Map<String, Object> params) {
        //构造pageNo pageSize
        IPage<AttendRecordEntity> page = new QueryUtil<AttendRecordEntity>().getQueryPage(params);

        List<AttendRecordEntity> list = baseMapper.queryPage(page,params);
        page.setRecords(list);

        return new PageUtil(page);
    }

    /**
    * @Description: selectAll 获取考勤列表数据(模糊查询)
    * @Param: [params]
    * @return: java.util.List<com.tony.pmp.model.entity.AttendRecordEntity>
    * @Author: 皮蛋布丁
    * @Date: 2021/8/4 11:46
    */
    @Override
    public List<AttendRecordEntity> selectAll(Map<String, Object> params) {
        return baseMapper.selectExportData(params);
    }

    /**
    * @Description: manageExport 中间站转换处理为导出服务
    * @Param: [list]
    * @return: java.util.List<java.util.Map<java.lang.Integer,java.lang.Object>>
    * @Author: 皮蛋布丁
    * @Date: 2021/8/4 14:25
    */
    @Override
    public List<Map<Integer, Object>> manageExport(List<AttendRecordEntity> list) {
        List<Map<Integer,Object>> listMap = Lists.newArrayList();
        //"ID","部门名称","姓名","日期","打卡状态","打卡开始时间","打卡结束时间","工时/小时"

        if (list != null && !list.isEmpty()) {
            list.stream().forEach(entity -> {
                try {
                    Map<Integer,Object> rowMap = Maps.newHashMap();

                    rowMap.put(0,entity.getId());
                    rowMap.put(1,entity.getDeptName());
                    rowMap.put(2,entity.getUserName());
                    rowMap.put(3, Constant.DATE_FORMAT.format(entity.getCreateTime()));
                    Constant.AttendStatus status =
                            Constant.AttendStatus.byCode(entity.getStatus().intValue());
                    rowMap.put(4,status != null? status.toString() : "空");
                    rowMap.put(5,Constant.SIMPLE_DATE_FORMAT.format(entity.getStartTime()));
                    rowMap.put(6,Constant.SIMPLE_DATE_FORMAT.format(entity.getEndTime()));
                    rowMap.put(7,entity.getTotal());

                    listMap.add(rowMap);
                } catch (Exception e) {}
            });
        }
        return listMap;
    }
}
